package com.tosh;

import com.tosh.model.Bean;
import com.tosh.model.ConstructorArg;
import com.tosh.model.Property;

import java.util.Collections;
import java.util.List;

import static com.tosh.Utils.isNOE;

/**
 * User: arsentyev
 * Date: 20.08.12
 */
class HelpUtils {
    public static Bean buildBean(String id, String clazz, List<Property> properties, List<ConstructorArg> constructorArgs) {
        if(isNOE(id) || isNOE(clazz)) {
            return null;
        }

        if(properties == null) {
            properties = Collections.emptyList();
        }
        if(constructorArgs == null) {
            constructorArgs = Collections.emptyList();
        }

        return new Bean(id, clazz, properties, constructorArgs);
    }

    public static Property buildProperty(String name, String ref) {
        if(isNOE(name) || isNOE(ref)) {
            return null;
        } else {
            return new Property(name, ref);
        }
    }

    public static ConstructorArg buildConstructorArg(int index, String value) {
        if(isNOE(value)) {
            return null;
        } else {
            return new ConstructorArg(index, value);
        }
    }

    public static void fixArgs(List<ConstructorArg> args) {
        ConstructorArg temp = null, ca = null;
        int index = 1, size = args.size();

        for(int i = 0; i < size; i++) {
            ca = args.get(i);
            index = ca.getIndex();
            if(index != i) {
                if(index > size) {
//                    continue;
                }
                temp = args.get(index);
                if(temp != null) {
                    args.set(i, temp);
                    args.set(index, ca);
                    i--;
                    continue;
                }

                args.set(size-1, ca);
                args.remove(i);
                i--;
            }
        }
    }
}